package com.game.majiang.vo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;

public class WeChatLogin {

	private static String APP_ID="";//
	private static String APP_SECRIT="";//
	
	public static String getToken(String code) {
		  URL url = null;
		    String postData = null;
		    String json = "";
		    APP_ID = DataManager.getInstance().systemValue("APP_ID");
		    APP_SECRIT = DataManager.getInstance().systemValue("APP_SECRIT");
		    try {	    	
		        postData ="appid=" + APP_ID + "&secret=" + APP_SECRIT + "&code=" + code + "&grant_type=authorization_code";
		     	url = new URL(" https://api.weixin.qq.com/sns/oauth2/access_token");
		    }
		   catch (Exception e1) {
		     e1.printStackTrace();
		     return "";
		    }
		   
			
		    try {
		        //发送POST请求
		    	
		        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		        conn.setRequestMethod("GET");
		        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		        conn.setRequestProperty("Connection", "Keep-Alive");
		        conn.setUseCaches(false);
		        conn.setDoOutput(true);

		        conn.setRequestProperty("Content-Length", "" + postData.length());
		        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
		        out.write(postData);
		        out.flush();
		        out.close();

		        //获取响应状态
		        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
		        	LogManager.getInstance().Log("-----------connect failed!");
//		            LogManager.getInstance().Log("connect failed!");
		         
		        }
		        //获取响应内容体
		        String line, result = "";
		        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
		        while ((line = in.readLine()) != null) {
		            result = line ;
		        }
		        in.close();
		       
		        LogManager.getInstance().Log("-------------result:"+result);
//		        LogManager.getInstance().Log("result:"+result);
		        json = result;
//		        JSONObject jsonObj = new JSONObject(result);
//				String token = jsonObj.getString("access_token");
//				String openid = jsonObj.getString("openid");

		    } catch (IOException e) {
		        e.printStackTrace(System.out);
		        return "";
		    }
			return json;
	}
	public static String getWeChatInfo(String access_token, String openid) {
		  URL url = null;
		    String postData = null;
		    String json = "";
		    try {	    	
		        postData ="access_token=" + access_token + "&openid=" + openid ;
		     	url = new URL(" https://api.weixin.qq.com/sns/userinfo");
		    }
		   catch (Exception e1) {
		     e1.printStackTrace();
		     return "";
		    }
		   
			
		    try {
		        //发送POST请求
		        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		        conn.setRequestMethod("GET");
		        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		        conn.setRequestProperty("Connection", "Keep-Alive");
		        conn.setUseCaches(false);
		        conn.setDoOutput(true);

		        conn.setRequestProperty("Content-Length", "" + postData.length());
		        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
		        out.write(postData);
		        out.flush();
		        out.close();

		        //获取响应状态
		        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
		        	LogManager.getInstance().Log("-----------connect failed!");
		            LogManager.getInstance().Log("connect failed!");
		         
		        }
		        //获取响应内容体
		        String line, result = "";
		        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
		        while ((line = in.readLine()) != null) {
		            result = line ;
		        }
		        in.close();	       
		        LogManager.getInstance().Log("-------------result:"+result);
//		        LogManager.getInstance().Log("result:"+result);
		        json = result;
		       

		    } catch (IOException e) {
		        e.printStackTrace(System.out);
		        return "";
		    }
			return json;
	}
	public static String getWeChat(String code) {
//		code = "071RUEi022pFVS0hfsi02581j02RUEiY";
		String json = getToken(code);
		if ("".equals(json)) {
			return "";
		}
		 JSONObject jsonObj = new JSONObject(json);
		 String jsonString = jsonObj.toString();
		 if (jsonString.indexOf("access_token") == -1) {
			 return "";
		}
		 String str = "";
		 	try {
		 		String token = jsonObj.getString("access_token");
				String openid = jsonObj.getString("openid");
				String jsoninfo = getWeChatInfo(token,openid);
				if ("".equals(json)) {
					return "";
				}
//				LogManager.getInstance().Log(jsoninfo);
				str = jsoninfo;
			} catch (Exception e) {
				e.printStackTrace();
				return "";
			}
			
			
			
//			 JSONObject jsonObjInfo = new JSONObject(jsoninfo);
//				String openid2 = jsonObj.getString("openid");
//				String unionid = jsonObj.getString("unionid");
//				String nickname = jsonObj.getString("nickname");
			return str;
	}
}
